<?php

class FormController extends BaseAdminController
{

  protected $Form;

  protected $gateway;

  protected $table;

  protected $pk;

  protected $limit = 10;

  protected $title;

  protected $position;

  protected $sortField;

  protected $sortDirection;

  protected $elements;

  protected $filters;

  protected $rules;

  protected function getTable()
  {
    if (empty($this->table)) {
      $this->table = $this->getName();
    }

    return $this->table;
  }

  protected function getTitle()
  {
    if (empty($this->title)) {
      $this->title = Inflector::titleize($this->getName());
    }

    return $this->title;
  }

  protected function getPk()
  {
    if (empty($this->pk)) {
      $this->pk = Inflector::singularize($this->getTable()) . '_id';
    }

    return $this->pk;
  }

  protected function initialize()
  {
    parent::initialize();

    $this->Form = new Form();
    $this->Form->setTitle($this->getTitle());
    $this->Form->setTable($this->getTable());
    $this->Form->setPk($this->getPk());

    foreach ((array)$this->elements as $element) {
      $class = 'FormElement_' . $element[0];
      $name = $element[1];
      $title = sy_get_param($element, 2);

      $Element = new $class($name, $title);

      foreach ((array)sy_get_param($element, 3) as $param => $value) {
        $Element->$param = $value;
      }

      $this->Form->addElement($Element, sy_get_param($element, 4, Form::ACTION_ALL));
    }

    foreach ((array)$this->filters as $filter) {
      $class = 'FormFilter_' . $filter[0];
      $name = $filter[1];
      $title = sy_get_param($filter, 2);

      $Filter = new $class($name, $title);

      foreach ((array)sy_get_param($filter, 3) as $param => $value) {
        $Filter->$param = $value;
      }

      $this->Form->addFilter($Filter);
    }
  }

  protected function indexAction()
  {
    try {
      $this->Form->getAction(Form::ACTION_INDEX)->sortField = $this->sortField;
      $this->Form->getAction(Form::ACTION_INDEX)->sortDirection = $this->sortDirection;

      $this->Form->execute(Form::ACTION_INDEX);
    }
    catch (ValidationException $e) {
      Simplify::getInstance()->warnings($e->getErrors());
    }

    return $this->Form->render(Form::ACTION_INDEX);
  }

  protected function viewAction()
  {
    try {
      $this->Form->execute(Form::ACTION_VIEW);
    }
    catch (ValidationException $e) {
      Simplify::getInstance()->warnings($e->getErrors());
    }
    catch (NotFoundException $e) {
      Simplify::getInstance()->warnings($e->getMessage());

      return $this->response->redirect(Simplify::request()->route());
    }

    $this->Form->getAction(Form::ACTION_VIEW)->sortField = $this->sortField;
    $this->Form->getAction(Form::ACTION_VIEW)->sortDirection = $this->sortDirection;

    return $this->Form->render(Form::ACTION_VIEW);
  }

  protected function bulkAction()
  {
    $action = $this->request->post()->get('bulk_action');

    if (empty($action)) {
      Simplify::getInstance()->warnings('Select an action');
    }
    else {
      switch ($action) {
        case 'delete' :
          try {
            $this->Form->execute(Form::ACTION_DELETE);
          }
          catch (ValidationException $e) {
            Simplify::getInstance()->warnings($e->getErrors());
          }

          break;

        case 'view' :
          try {
            $this->Form->execute(Form::ACTION_VIEW);
          }
          catch (ValidationException $e) {
            Simplify::getInstance()->warnings($e->getErrors());
          }

          return $this->Form->render(Form::ACTION_VIEW);

          break;
      }
    }

    return $this->response->redirect(Simplify::request()->route());
  }

  protected function deleteAction()
  {
    try {
      $this->Form->execute(Form::ACTION_DELETE);
    }
    catch (ValidationException $e) {
      Simplify::getInstance()->warnings($e->getErrors());
    }
    catch (NotFoundException $e) {
      Simplify::getInstance()->warnings($e->getMessage());

      return $this->response->redirect(Simplify::request()->route());
    }

    return $this->response->redirect(Simplify::request()->route());
  }

  protected function createAction()
  {
    if (($output = $this->form(Form::ACTION_CREATE)) === true) {
      Simplify::getInstance()->notices('Record saved');

      return $this->response->redirect(Simplify::request()->route());
    }

    return $output;
  }

  protected function editAction()
  {
    if (($output = $this->form(Form::ACTION_EDIT)) === true) {
      Simplify::getInstance()->notices('Record saved');

      return $this->response->redirect(Simplify::request()->route());
    }

    return $output;
  }

  protected function form($action, $id = null)
  {
    $this->Form->setId($id);
    $this->Form->getAction($action)->rules = $this->rules;

    try {
      if ($this->Form->execute($action) === true) {
        return true;
      }
    }
    catch (ValidationException $e) {
      Simplify::getInstance()->warnings($e->getErrors());
    }
    catch (NotFoundException $e) {
      Simplify::getInstance()->warnings($e->getMessage());

      return $this->response->redirect(Simplify::request()->route());
    }

    return $this->Form->render($action);
  }

}

?>